package com.stdio.ui;

import com.stdio.R;
import com.stdio.modle.PBInfor;
import com.stdio.modle.PeopleInfo;
import com.stdio.view.ContactDetailView;
import com.stdio.view.ContactEditTagView;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ContactEditActivity extends Activity {
	
	private PBInfor mPbTool;
	private LayoutInflater mInflater;
	private LinearLayout layout1;
	private LinearLayout phonetitle;
	private LinearLayout layout2;
	private LinearLayout emailtitle;
	private ContactEditTagView mEditview;
	private EditText mContacttext;
	private ImageButton backtobutton;
//	private ImageView delIcon;
	private Button mSavebutton;
	private EditText mPhonetext;
	private EditText mEmail;
	private PeopleInfo mContactInfo;
	private String mContactname;
	private String sActionintent;
	private String sContactId;
	private MyOnClickListener listener;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_contact_edit);
		
		
		
		mInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
		layout1 = (LinearLayout)findViewById(R.id.personalcard_item_layout1);
		phonetitle = (LinearLayout)findViewById(R.id.phone_title);
		layout2 = (LinearLayout)findViewById(R.id.personalcard_item_layout2);
		emailtitle = (LinearLayout)findViewById(R.id.email_title);
		
		layout1 = (LinearLayout)findViewById(R.id.personalcard_item_layout1);
		phonetitle = (LinearLayout)findViewById(R.id.phone_title);
		layout2 = (LinearLayout)findViewById(R.id.personalcard_item_layout2);
		emailtitle = (LinearLayout)findViewById(R.id.email_title);
		//mEditview = (ContactEditTagView)mInflater.inflate(R.layout.layout_personalcard_item_edit, null);
		//mEditview = (ContactEditTagView)findViewById();
		
		mContacttext = (EditText)findViewById(R.id.contact_name);
		mPhonetext = (EditText)findViewById(R.id.personalcard_item_content);
		mEmail = (EditText)findViewById(R.id.personalcard_item_content3);
		
		mSavebutton = (Button)findViewById(R.id.btn_save);
		backtobutton = (ImageButton)findViewById(R.id.back_toContact); 
		
		listener = new MyOnClickListener();
		mSavebutton.setOnClickListener(listener);
		backtobutton.setOnClickListener(listener);
		
		if("android.intent.action.EDIT".equals(sActionintent))
		{
			Intent intent = getIntent();
			sActionintent = intent.getAction();
		
			Bundle bundle = intent.getExtras();
			sContactId = bundle.getString("contactid");
			initComponent(sContactId);
		}
		
		mSavebutton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mContactInfo.setDisplayName(mContacttext.getText().toString());
				mContactInfo.setPhone(mPhonetext.getText().toString());
				mContactInfo.setEmail(mPhonetext.getText().toString());
				if("android.intent.action.EDIT".equals(sActionintent))
				{
					mPbTool.editContact(mContactInfo);
				}
				if("android.intent.action.INSERT".equals(sActionintent))
				{
					mPbTool.addContact(mContactInfo);
				}
				finish();
			}
			
		});
		
	}
	public void initComponent(String sContactId)
	{
		mPbTool = new PBInfor(this);
		mContactInfo = mPbTool.getContact(sContactId);
		mContactname = mContactInfo.getDisplayName();
		if(mContactname!=null&&!"".equals(mContactname))
		{
			mContacttext.setText(mContactname);
		}
		String sContactphone = mContactInfo.getPhone();
		if(sContactphone!=null&&!"".equals(sContactphone))
		{
			mPhonetext.setText(sContactphone);
		}
		else 
		{
			phonetitle.setVisibility(View.GONE);
			layout1.setVisibility(View.GONE);
		}
		String sEmail = mContactInfo.getEmail();
		if(sEmail!=null&&!"".equals(sEmail))
		{
			mEmail.setText(sEmail);
		}
		else
		{
			emailtitle.setVisibility(View.GONE);
			layout2.setVisibility(View.GONE);
		}
	}
	
	private class MyOnClickListener implements View.OnClickListener
	{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			final Intent intent = new Intent(ContactEditActivity.this,ContactsUI.class);
			switch(v.getId())
			{
				case R.id.back_toContact:
					Builder builder = new Builder(ContactEditActivity.this);
					builder.setTitle("Notics");
					builder.setMessage("Do you want to absort this setting?");
					builder.setPositiveButton("Ok", new AlertDialog.OnClickListener(){

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							startActivity(intent);
						}
						
					});
					builder.setCancelable(true);
					builder.create();
					builder.show();	
					builder.setNegativeButton("Cancle", new AlertDialog.OnClickListener(){

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							dialog.cancel();
						}
						
					});
					builder.show();	
					
					break;
				case R.id.btn_save:
					mContactInfo.setDisplayName(mContacttext.getText().toString());
					mContactInfo.setPhone(mPhonetext.getText().toString());
					mContactInfo.setEmail(mPhonetext.getText().toString());
					if("android.intent.action.EDIT".equals(sActionintent))
					{
						mPbTool.editContact(mContactInfo);
					}
					if("android.intent.action.INSERT".equals(sActionintent))
					{
						mPbTool.addContact(mContactInfo);
					}
					startActivity(intent);
					break;
			}
		}
		
	}

}
